package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    private ExtCproductService extCproductService;

    @Reference
    private FactoryService factoryService;

    /*
    url：/cargo/extCproduct/list.do?contractId=fca56fc5-77ce-4e13-a358-141ce6cb49d5&contractProductId=1c13ecca-ca33-4b8f-85dc-1ac161487b78
    作用：查询附件列表
    参数： contractId 购销合同的id,contractProductId 货物的id
    返回值： 附件列表页面(cargo/extc/extc-list.jsp)
  */
    @RequestMapping("/list")
    public String list(String contractId,String contractProductId,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        //1. 查询货物的附件列表
        //创建附件的条件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        //把该购销合同的附件查询出来
        extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo<ExtCproduct> pageInfo = extCproductService.findByPage(extCproductExample,pageNum,pageSize);
        request.setAttribute("pageInfo",pageInfo);

        //2., 查询生产附件的厂家
        FactoryExample factoryExample = new FactoryExample();
        //添加条件
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        //3. 把购销合同的id存储到请求域中
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        //3. 回到页面
        return "cargo/extc/extc-list";

    }



    @Autowired
    private FileUploadUtil fileUploadUtil;

    /*
     url：/cargo/extCproduct/edit.do
     作用：保存添加与保存修改
     参数：附件对象
     返回值： 附件列表
  */
    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct,MultipartFile productPhoto) throws Exception { //文件上传的字段的数据类型一定要是MultipartFile
        //补充数据，因为这些数据是来自于登陆者
        String companyId = getLoginUserCompanyId();
        String companyName= getLoginUserCompanyName();
        extCproduct.setCompanyId(companyId);
        extCproduct.setCompanyName(companyName);
        //附件的创建人
        extCproduct.setCreateBy(getLoginUser().getId());
        //附件创建人所属的部门
        extCproduct.setCreateDept(getLoginUser().getDeptId());

        //通过上传文件的大小判断用户是否有上传
        if(productPhoto.getSize()>0){
            //1、把文件上传到七牛云，附件对象保存七牛云的图片的地址
            String url = fileUploadUtil.upload(productPhoto); //返回的就是图片在七牛上的地址
            extCproduct.setProductImage("http://"+url);
        }

        //1.判断extCproduct是否携带id过来，如果没有携带则是添加
        if(StringUtils.isEmpty(extCproduct.getId())){
            extCproductService.save(extCproduct);
        }else {
            //2. 如果携带id过来则是修改
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }


    /*
        url：/cargo/extCproduct/toUpdate.do?id=06030157-0ab3-4cd7-a5b9-6f72751d8334&contractId=fca56fc5-77ce-4e13-a358-141ce6cb49d5&contractProductId=1c13ecca-ca33-4b8f-85dc-1ac161487b78
        作用：进入附件修改页面
        参数：附件id , contractId 购销合同id，contractProductId 货物的id
        返回值：cargo/extCproduct/extCproduct-update.jsp
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id,String contractId,String contractProductId){
        //1. 根据id查询附件
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);

        //2. 厂家
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        //3. 返回更新页面
        return "cargo/extc/extc-update";

    }

    /*
     url：/cargo/extCproduct/delete.do?id=${o.id}&contractId=${contractId}&contractProductId=${o.contractProductId}
     作用：删除附件
     参数：附件id, 购销合同的id , contractProductId 货物的id
     返回值：附件列表
  */
    @RequestMapping("/delete")
    public String delete(String id,String contractId,String contractProductId){
         extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }



}
